<template>
  <div class="home">
    <div class="home-body">
      <div class="left"><img src="../assets/login.png" alt="" /></div>
      <div class="right">
        <h1>欢迎登录</h1>
        <input
          v-focus
          placeholder="请输入您的用户名"
          v-model="user.username"
        /><br />
        <input
          type="password"
          placeholder="请输入您的密码"
          v-model="user.password"
        />
        <h4>
          还没有账户？<router-link to="/register">新用户注册</router-link>
        </h4>
        <button @click="login">登录</button>
      </div>
    </div>
  </div>
</template>
<script>
import { mapMutations, mapState } from "vuex";
export default {
  name: "Home",
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
    };
  },
  computed: {
    ...mapState({
    }),
  },
  methods: {
    ...mapMutations(["updateLogin","update"]),
    async login() {
      if (this.user.username === "" || this.user.password === "") {
        alert("账户或密码不能为空，请重新输入");
      } else {
        const data = await this.yPost("/user/login", {
          username: this.user.username,
          password: this.user.password,
        });
        console.log(data);
        if (data) {
          if (data.status == 200) {
            localStorage.setItem("userInfo", JSON.stringify(data.data.user)),
            this.update(data.data.user)
              this.$router.push({
                name: "user",
              });
          }
        } else {
          alert("用户名或密码错误！");
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.home {
  width: 1100px;
  height: 700px;
  margin: 5% auto;
  border-radius: 5px;
  box-shadow: 0px 0px 2px 1px rgb(0 0 0 / 20%);
  text-align: center;
  &-body {
    width: 1000px;
    height: 90%;
    margin: auto;
    padding-top: 45px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      width: 500px;
      height: 600px;
    }
  }
}
.right {
  padding-left: 60px;
  width: 500px;
  input {
    width: 80%;
    height: 40px;
    margin-top: 20px;
    border-radius: 10px;
    text-indent: 1em;
    outline: none;
  }
  button {
    width: 70%;
    background-color: rgb(86, 84, 218);
    height: 40px;
    border-radius: 10px;
    color: #fff;
  }
}
</style>